//移动节点页面
function movenode(id){
	var url = "frontend/movedep.asp?id="+id+"&timestamp="+ new Date().getTime();
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange1;
    xmlHttp.open("get",url,true);
    xmlHttp.send(null);
	document.all.topoperation.innerHTML = "移动";
}

//获取动态级联菜单
function changefathernodeid(id,fatherid){
	var url = "frontend/getsublist.asp?id="+id+"&fatherid="+fatherid+"&rootid="+document.getElementById("rootnodeid").value+"&timestamp="+ new Date().getTime();
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange3;
    xmlHttp.open("get",url,true);
    xmlHttp.send(null);
}

function handleStateChange3() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
			var ajaxRequestObj = xmlHttp.responseText;
			document.getElementById("thesublist").innerHTML = ajaxRequestObj;
        }
    }
}

//移动节点
function submitmove(id){
	var fatherid = document.getElementById("fathernodeid").value;
	var rootid = document.getElementById("rootnodeid").value;
	var url = "backend/movenode.asp?id="+id+"&fatherid="+fatherid+"&rootid="+rootid+"&timestamp="+ new Date().getTime();
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handlemovenodeStateChange;
    xmlHttp.open("get",url,true);
    xmlHttp.send(null);
}

function handlemovenodeStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
			var ajaxRequestObj = xmlHttp.responseText;
			if (ajaxRequestObj=="same"){
				showmsg("a","移动操作最后结果","没有移动操作！");
			}else{
				if (ajaxRequestObj=="err"){
					showmsg("a","移动操作最后结果","移动失败！");
				}else{
					if(ajaxRequestObj=="double"){
						showmsg("a","移动操作最后结果","单位重复，请使用合并单位功能！");
					}else{
						var rootid = ajaxRequestObj.split("|");
						var theli = document.getElementById("dep"+rootid[1]);
						refreshfinish = false;
						if (rootid[0]==rootid[1] || theli==null){
							refreshdep(rootid[0]);
							check4msg(rootid[2],rootid[3]);
						}
						else{
							refreshdep(rootid[1]);
							checkfinish(rootid[0],rootid[2],rootid[3]);
						}
					}
				}
			}
			document.all.topoperation.innerHTML = "移动";
			removeshadeandoperate();
        }
    }
}
//刷新节点
function refreshdep(rootid){
	var theli = document.getElementById("dep"+rootid);
	var thea = theli.getElementsByTagName("a");
	if (typeof(theli.getElementsByTagName("ul")[0])=="object"){
		theli.getElementsByTagName("ul")[0].removeNode(true);
	}
	thea[0].className="close";
	switchclass(rootid,thea[0],0);
	innerloadingtext(thea[1]);
}

function check4msg(oldfathername,fathername){
	if (refreshfinish){
		refreshfinish=false;setTimeout("showmsg('a','移动操作最后结果','移动成功！<br>把[<b>"+oldfathername+"</b>]移动到<br>[<b>"+fathername+"</b>]下');",79);
	}else{
	setTimeout("check4msg('"+oldfathername+"','"+fathername+"');",179);
	}
}
function checkfinish(rootid,oldfathername,fathername){
	if (refreshfinish){
		refresh2(rootid,oldfathername,fathername);
	}else{
	setTimeout("checkfinish("+rootid+",'"+oldfathername+"','"+fathername+"');",66);
	}
}
function refresh2(rootid,oldfathername,fathername){
	refreshdep(rootid);
	check4msg(oldfathername,fathername);
}
//合并节点页面
function unitenode(id,name){
	var url = "frontend/unitedep.asp?id="+id+"&timestamp="+ new Date().getTime();
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange1;
    xmlHttp.open("get",url,true);
    xmlHttp.send(null);
	document.all.topoperation.innerHTML = "合并";
	//alert(id);
	document.getElementById("manage_bottom"+id).innerHTML = "注意：合并将会删除并将<font color=\"#8F1073\">【"+name+"】</font>所有下级单位和用户移动到新的单位下！";
}
//合并节点
function submitunite(id){
	var aimid = document.getElementById("fathernodeid").value;
	var url = "backend/unitenode.asp?id="+id+"&aimid="+aimid+"&timestamp="+ new Date().getTime();
	if(aimid=="-1"){
		showmsg("a","合并操作最后结果","请确定目标单位！");
	}else{
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleuniteStateChange;
	    xmlHttp.open("get",url,true);
    	xmlHttp.send(null);
	}
}
function handleuniteStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
			var ajaxRequestObj = xmlHttp.responseText;
			if (ajaxRequestObj=="err"){
				showmsg("a","合并操作最后结果","合并失败，错误的操作！");
			}else{
				var c = ajaxRequestObj.split("|");
				refreshdep(c[0]);
				showmsg("a","合并操作最后结果","合并成功！<br>把[<b>"+c[1]+"</b>]合并到<br>[<b>"+c[2]+"</b>]下");
			}
			removetag("dep"+c[3]);
			depdetail(1,c[4]);
			document.all.topoperation.innerHTML = "合并";
			removeshadeandoperate();
        }
    }
}
